/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.StatusEnum;
import com.ykht.proj.coupon.Enum.TagStatus;
import com.ykht.proj.coupon.entity.CnLabel;
import com.ykht.proj.coupon.entity.FxCmsColumn;
import com.ykht.proj.coupon.service.CnLabelService;
import com.ykht.proj.coupon.vo.CardLabVo;
import com.ykht.proj.coupon.vo.CnLabelVo;
import com.ykht.proj.coupon.vo.FxCmsTagCount;
import com.ykht.proj.coupon.vo.FxCmsTagVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.activation.MailcapCommandMap;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 标签表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cnlabel")
@Api(value = "cnlabel", tags = "标签表管理")
@Slf4j
public class CnLabelController {

	private final CnLabelService cnLabelService;



	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param cnLabelVo 标签表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('coupon_cnlabel_view')")
	public R getCnLabelPage(Page page, CnLabelVo cnLabelVo) {
		log.info("分页:{}+查询:{}", page, cnLabelVo);
		QueryWrapper<CnLabel> queryWrapper = new QueryWrapper<>();
		if (null != cnLabelVo.getName()) queryWrapper.lambda().like(CnLabel::getName, cnLabelVo.getName());
		if (null != cnLabelVo.getUpdateStartDate())
			queryWrapper.lambda().ge(CnLabel::getUpdateTime, cnLabelVo.getUpdateStartDate());
		if (null != cnLabelVo.getUpdateEndDate())
			queryWrapper.lambda().le(CnLabel::getUpdateTime, cnLabelVo.getUpdateEndDate());
		queryWrapper.lambda().orderByAsc(CnLabel::getSort);
		return R.ok(cnLabelService.page(page, queryWrapper));
	}
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/listByids")
	public R getCnLabelPage(@RequestParam("list") List<Long> list) {
		log.info("根据ids查询标签：{}",list );
		return R.ok(cnLabelService.listByIds(list));
	}
	@PostMapping("/listByCardLab")
	public R< List<CardLabVo>> listByCardLab(@RequestBody List<CardLabVo> list) {
		log.info("根据ids查询标签：{}",list );

		List<Long> ids = list.stream().map(CardLabVo::getLabelId).distinct().collect(Collectors.toList());
		if (ids.size()==0) return R.ok(list);
		List<CnLabel> labelList = cnLabelService.listByIds(ids);
		Map<Long,String> map = labelList.stream().collect(Collectors.toMap(o->o.getId(),o->o.getName(),(k, v)->v));
		list.stream().forEach(o->{
			o.setLabelName(map.get(o.getLabelId()));
		});
		return R.ok(list);
	}

//	public static void main(String[] args) {
//		List<CnLabel> labelList = new ArrayList<>();
//		CnLabel cnLabel = new CnLabel();
//		cnLabel.setId(1l);
//		cnLabel.setName("111112");
//		labelList.add(cnLabel);
//		Map<Long,String> map = labelList.stream().collect(Collectors.toMap(o->o.getId(),o->o.getName(),(k, v)->v));
//		System.out.println(map.get(1l));
//	}

	/**
	 * 计数查询
	 *
	 * @return
	 */
	@ApiOperation(value = "计数查询", notes = "计数查询")
	@GetMapping("/getCnLabelCount")
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmssource_view')" )
	public R getCnLabelCount(CnLabelVo cnLabelVo) {
		QueryWrapper<CnLabel> queryWrapper = new QueryWrapper<>();
		if (null != cnLabelVo.getName()) queryWrapper.lambda().like(CnLabel::getName, cnLabelVo.getName());
		if (null != cnLabelVo.getUpdateStartDate())
			queryWrapper.lambda().ge(CnLabel::getUpdateTime, cnLabelVo.getUpdateStartDate());
		if (null != cnLabelVo.getUpdateEndDate())
			queryWrapper.lambda().le(CnLabel::getUpdateTime, cnLabelVo.getUpdateEndDate());

		// 上架统计
		queryWrapper.lambda().eq(CnLabel::getShowFlag, 0);
		cnLabelVo.setPutAway(cnLabelService.count(queryWrapper));
		// 下架统计
		queryWrapper.lambda().eq(CnLabel::getShowFlag, 1);
		cnLabelVo.setSoldOut(cnLabelService.count(queryWrapper));
		return R.ok(cnLabelVo);
	}

	/**
	 * 通过id查询标签表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_cnlabel_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(cnLabelService.getById(id));
	}

	/**
	 * 新增标签表
	 *
	 * @param cnLabel 标签表
	 * @return R
	 */
	@ApiOperation(value = "新增标签表", notes = "新增标签表")
	@SysLog("新增标签表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_cnlabel_add')")
	public R save(@RequestBody CnLabel cnLabel) {
		cnLabel.setCreateTime(LocalDateTime.now());
		cnLabel.setCreateUser(SecurityUtils.getUserId());
		return cnLabelService.insertCnLabel(cnLabel);
	}

	/**
	 * 修改标签表
	 *
	 * @param cnLabel 标签表
	 * @return R
	 */
	@ApiOperation(value = "修改标签表", notes = "修改标签表")
	@SysLog("修改标签表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_cnlabel_edit')")
	public R updateById(@RequestBody CnLabel cnLabel) {
		cnLabel.setUpdateTime(LocalDateTime.now());
		cnLabel.setUpdateUser(SecurityUtils.getUserId());
		return cnLabelService.updateLabel(cnLabel);
	}

	/**
	 * 通过id删除标签表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除标签表", notes = "通过id删除标签表")
	@SysLog("通过id删除标签表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_cnlabel_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(cnLabelService.removeById(id));
	}


	/**
	 * 是否展示
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "是否展示")
	@GetMapping("exhibition")
	public R exhibition(Long id) {
		CnLabel cnLabel = cnLabelService.getById(id);
		UpdateWrapper<CnLabel> updateWrapper = new UpdateWrapper<>();
		if (cnLabel.getShowFlag().equals(StatusEnum.normal.getValue())) {
			updateWrapper.lambda().set(CnLabel::getShowFlag, StatusEnum.forbid.getValue());
		} else {
			updateWrapper.lambda().set(CnLabel::getShowFlag, StatusEnum.normal.getValue());
		}
		updateWrapper.lambda().eq(CnLabel::getId, id);
		return R.ok(cnLabelService.update(updateWrapper));
	}
}
